package cola.rh.System.Service.impl;

import cola.rh.System.Entity.Role;
import cola.rh.System.Entity.UserRole;
import cola.rh.System.Entity.vo.AssginRoleVo;
import cola.rh.System.Entity.vo.RoleQueryVo;
import cola.rh.System.Mapper.RoleMapper;
import cola.rh.System.Mapper.UserRoleMapper;
import cola.rh.System.Service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Name: RoleServiceImpl
 * @Author: Cola
 * @Time: 2023/3/8 15:34
 * @Description: RoleServiceImpl
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public IPage<Role> pageQueryUser(Long page, Long limit, RoleQueryVo roleQueryVo) {
           /*
        创建 PageInfo 对象
        */
        Page<Role> pageInfo = new Page<>(page, limit);

        /*
        封装查询条件
        */
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        String roleName = roleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            /*条件存在，进行封装*/
            wrapper.like(Role::getRoleName, roleName);
        }

        IPage<Role> pageModel = this.page(pageInfo, wrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> findRoleByUserId(Long userId) {

        /*查询所有 Role 列表*/
        List<Role> allRoleList = this.list();

        /*查询该 userId 对应的 UserRole 列表*/
        List<UserRole> existUserRoleList = userRoleMapper.selectList(
                new LambdaQueryWrapper<UserRole>().
                        eq(UserRole::getUserId, userId).
                        select(UserRole::getRoleId)
        );

        /* 该 userId 具有的 roleId 列表*/
        List<Long> exitRoleIdList = existUserRoleList.stream().map(
                userRole -> userRole.getRoleId()
        ).collect(Collectors.toList());

        /*该 userId 具有的 roleId 与所有 roleId 的交集即为 该 userId 所拥有的 roleId*/
        List<Role> assginRoleList = new ArrayList<>();

        allRoleList.forEach(role -> {
            if (exitRoleIdList.contains(role.getId())) {
                assginRoleList.add(role);
            }
        });

        Map<String, Object> roleMap = new HashMap<>();

        roleMap.put("assginRoleList", assginRoleList);
        roleMap.put("allRoleList", allRoleList);
        return roleMap;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        /*先删除该 userId 下所有的 UserRole*/
        userRoleMapper.delete(
                new LambdaQueryWrapper<UserRole>().
                        eq(UserRole::getUserId, assginRoleVo.getUserId()));

        /*遍历前端传过来的 roleIdList 如果存在 roleId 就插入*/
        for (Long roleId : assginRoleVo.getRoleIdList()) {
            if (StringUtils.isEmpty(roleId)) continue;
            UserRole userRole = new UserRole();
            userRole.setUserId(assginRoleVo.getUserId());
            userRole.setRoleId(roleId);
            userRoleMapper.insert(userRole);
        }
    }
}
